@extends('admin.index')
@section('title','用户添加')
 @section('con')
 <div class="row">
 	@if (count($errors) > 0)
	    <div class="alert alert-danger">
	        <ul>
	            @foreach ($errors->all() as $error)
	                <li>{{ $error }}</li>
	            @endforeach
	        </ul>
	    </div>
	@endif
    <div class="col-lg-6 col-lg-offset-3">
        <form role="form" method="post" action="{{url('/admin/user/insert')}}" enctype="multipart/form-data">
            <div class="form-group">
                <label>账号</label> <span></span>
                <input class="form-control" readmin="请输入6-18位账号" type="text" name="username" value="">
                <p class="help-block"></p>
            </div>
            <div class="form-group">
                <label>密码</label> <span></span>
                <input class="form-control" readmin="请输入6-18位密码" type="password" name="password" value="">
                <p class="help-block"></p>
            </div>
            @if($users->auth == 5)
            <div class="form-group">
                <label>权限</label>
                <select class="form-control" name='auth'>
                    <option value='1'>普通用户</option>
                    <option value='2'>vip用户</option>
                    <option value='3'>广告用户</option>
                    <option value='4'>管理员</option>
                    <option value='5'>超级管理员</option>
                </select>
                <p class="help-block"></p>
            </div>
            @elseif($users->auth == 4)
            <div class="form-group">
                <label>权限</label>
                <select class="form-control" name='auth'>
                    <option value='1'>普通用户</option>
                    <option value='2'>广告用户</option>
                </select>
                <p class="help-block"></p>
            </div>
            @endif
            {{ csrf_field() }}
            <button class="btn btn-default" type="submit">提交</button>
            <button class="btn btn-default" type="reset">重置</button>
        </form>
    </div>
 </div>  
 @endsection
 @section('js')
 <script type="text/javascript">
     $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
    });

    //全局变量
    var UserisOk = true;
    var PasswordisOk = true;
    
    $('input').focus(function(){
        var t = $(this).attr('readmin');
        $(this).parent('div').removeClass('has-error').addClass('has-success').find('span').html(t).css('color','green');
    })

    //用户名丧失焦点
    $('input[name=username]').blur(function(){
        if($(this).val() != '' && $(this).val().length >=6 && $(this).val().length <=18){
            var inp = $(this);
            $.ajax({
                url:'/admin/user/select',
                data:{name:inp.val()},
                type:'get',
                success:function(data){
                    if(data==1){
                        // console.log(data);
                        inp.parent('div').find('span').html('');
                        UserisOk = true;
                    }else{
                        inp.parent('div').addClass('has-error').find('span').html('用户名已存在').css('color','red');
                        UserisOk = false;
                    }
                },
                async:false
            })
        }else{
            $(this).parent('div').addClass('has-error').find('span').html('请输入6-18位账号').css('color','red');
            UserisOk = false;
        }
    })

    $('input[name=password]').blur(function(){
        var reg = /^\w{6,18}$/;
        if(reg.test($(this).val())){
            PasswordisOk = true;
            $(this).parent('div').find('span').html('');
        }else{
            $(this).parent('div').addClass('has-error').find('span').html('请输入6-18位密码').css('color','red');
            PasswordisOk = false;
        }
    })

    $('form').submit(function(){
        $('input').trigger('blur');
        if(UserisOk && PasswordisOk){
            return true;
        }else{
            return false;
        }
    })
    </script>
@endsection